﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for RecordingControl.xaml
    /// </summary>
    public partial class RecordingControl : UserControl
    {
        StoredRecording recording;
        public delegate void VoidInvokeHandler();
        public RecordingControl(StoredRecording r)
        {
            InitializeComponent();

            recording = r;

            recording.NewTrackCreated += new StoredRecording.NewTrackCreatedEventHandler(NewTrackCreated);
            recording.Updated += new StoredRecording.VoidEventHandler(recording_Updated);

            Draw();
            DrawDiscs();
        }

        void recording_Updated()
        {
            Draw();
        }

        void recording_NewDiscCreated()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
                DiscsHolder.Children.Add(new DiscControl(recording.CurrentDisc));
            });
        }

        void NewTrackCreated()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
                RecordingScrollViewer.ScrollToBottom();
            });
        }

        void Draw()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
                RecordingDateTime.Content = "[" + recording.Id + "] " + recording.Date.ToString("ddd d MMMM yyyy HH:mm");
                Title.Content = recording.Title;
                MeetingTypeLabel.Content = SS.FullMeetingType(recording.Type);
            });
        }

        void DrawDiscs()
        {
            if (recording.Current)
            {
                recording.NewDiscCreated += new StoredRecording.NewDiscCreatedEventHandler(recording_NewDiscCreated);
            }
            foreach (StoredDisc disc in recording.Discs)
            {
                DiscsHolder.Children.Add(new DiscControl(disc));
            }
        }

        private void MeetingType_MouseDown(object sender, MouseButtonEventArgs e)
        {

            ChangeTypeWindow changeTypeWindow = new ChangeTypeWindow(recording.Type);
            recording.Type = changeTypeWindow.ChangeTypeDialog();
            MeetingTypeLabel.Content = SS.FullMeetingType(recording.Type);
        }

        private void Title_MouseDown(object sender, MouseButtonEventArgs e)
        {
            recording.Title = KeyboardEntryWindow.ShowKeyboard(recording.Title, "Recording Title");
        }

    }
}
